<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Tutorial: Autoupdating Collections</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Tutorial: Autoupdating Collections</h1>

    <section>

<header>
    

    <h2>Autoupdating Collections</h2>
</header>

<article>
    <h1>The Autoupdate Feature for Collections</h1><p>Autoupdate can be enabled on a per-collection basis via the constructor option <code>autoupdate: true</code>. The feature requires <code>Object.observe</code> (currently implemented in Chrome 36+, io.js and Node.js 0.12+). If observers are not available, the option will be ignored.</p>
<p>Autoupdate automatically calls <code>update(doc)</code> whenever a document is modified, which is necessary for index updates and dirty-marks (used to determine whether the DB has been modified and should be persisted).</p>
<p>Enabling this feature basically means, that all manual <code>update</code> calls can be omitted.</p>
<h2>Example</h2><pre class="prettyprint source lang-js"><code>var doc = collection.by(&quot;name&quot;, &quot;John&quot;);

doc.name = &quot;Peter&quot;;
doc.age = 32;
doc.gender = &quot;male&quot;;

collection.update(doc); // This line can be safely removed.</code></pre><p>Autoupdate will call <code>update</code> at the end of the current event loop cycle and thus only calls <code>update</code> once, even when multiple changes were made.</p>
<h2>Error handling</h2><p>There is one important difference between autoupdate and manual updates. If for example a document change violates a unique key constraint, <code>update</code> will synchronously throw an error which can be catched synchronously:</p>
<pre class="prettyprint source lang-js"><code>var collection = db.addCollection(&quot;test&quot;, {
  unique: [&quot;name&quot;]
});

collection.insert({ name: &quot;Peter&quot; });

var doc = collection.insert({ name: &quot;Jack&quot; });
doc.name = &quot;Peter&quot;;

try {
  collection.update(doc);
} catch(err) {
  doc.name = &quot;Jack&quot;;
}</code></pre><p>Since autoupdate calls <code>update</code> asynchronously, you cannot catch errors via <code>try-catch</code>. Instead you have to use event listeners:</p>
<pre class="prettyprint source lang-js"><code>var collection = db.addCollection(&quot;test&quot;, {
  unique: [&quot;name&quot;],
  autoupdate: true
});

collection.insert({ name: &quot;Peter&quot; });

var doc = collection.insert({ name: &quot;Jack&quot; });
doc.name = &quot;Peter&quot;;

collection.on(&quot;error&quot;, function(errDoc) {
  if(errDoc === doc) {
    doc.name = &quot;Jack&quot;;
  }
});</code></pre><p>This can become quite tedious, so you should consider performing checks before updating documents instead.</p>
</article>

</section>

</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Collection.html">Collection</a></li><li><a href="DynamicView.html">DynamicView</a></li><li><a href="Loki.html">Loki</a></li><li><a href="LokiEventEmitter.html">LokiEventEmitter</a></li><li><a href="LokiFsAdapter.html">LokiFsAdapter</a></li><li><a href="LokiFsStructuredAdapter.html">LokiFsStructuredAdapter</a></li><li><a href="LokiIndexedAdapter.html">LokiIndexedAdapter</a></li><li><a href="LokiLocalStorageAdapter.html">LokiLocalStorageAdapter</a></li><li><a href="LokiMemoryAdapter.html">LokiMemoryAdapter</a></li><li><a href="LokiPartitioningAdapter.html">LokiPartitioningAdapter</a></li><li><a href="Resultset.html">Resultset</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-Autoupdating Collections.html">Autoupdating Collections</a></li><li><a href="tutorial-Changes API.html">Changes API</a></li><li><a href="tutorial-Collection Transforms.html">Collection Transforms</a></li><li><a href="tutorial-Indexing and Query performance.html">Indexing and Query performance</a></li><li><a href="tutorial-Loki Angular.html">Loki Angular</a></li><li><a href="tutorial-Persistence Adapters.html">Persistence Adapters</a></li><li><a href="tutorial-Query Examples.html">Query Examples</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu May 10 2018 03:42:33 GMT-0400 (Eastern Daylight Time)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>